<template>
  <scroll class="listview"
          :probe-type="probeType"
          :data="data"
          :listenScroll="listenScroll"
          @scroll="scroll"
          ref="listview">
    <ul>
      <li class="list-group" v-for="(group,index) in data" :key="index" ref="listGroup">
        <!--        歌手分类标题-->
        <h2 class="list-group-title">{{group.title}}</h2>
        <ul>
          <!--          单个歌手信息-->
          <li @click="selectItem(item)" class="list-group-item" v-for="(item,index) in group.items" :key="index">
            <!--            歌手图片-->
            <img class="avatar" v-lazy="item.avatar" alt="图片丢失">
            <!--            歌手名字-->
            <span class="name">{{item.name}}</span>
          </li>
        </ul>
      </li>
    </ul>
    <!--    右侧导航字母表-->
    <div class="list-shortcut"
         @touchstart.stop.prevent="onShortcutTouchStart"
         @touchmove.stop.prevent="onShortcutTouchMove">
      <ul>
        <!--        点击时使用better-scroll封装好的touchstart事件-->
        <li
          v-for="(item,index) in shortcutList"
          class="item"
          :class="{'current':index === currentIndex}"
          :data-index="index"
          :key="index"
        >
          {{item}}
        </li>
      </ul>
    </div>
    <!--    左侧滚动时上面的标题-->
    <div class="list-fixed" v-show="fixTilte" ref="fixedTitle">
      <h1 class="fixed-title">{{fixTilte}}</h1>
    </div>
    <div v-show="!data.length" class="loading-container">
      <Loading></Loading>
    </div>
  </scroll>
</template>

<script type="text/ecmascript-6">
  import Scroll from 'base/scroll/scroll'
  import Loading from 'base/loading/loading'

  // 每个右侧列表的元素高度18px
  const ANCHOR_HEIGHT = 18
  // 标题的高度
  const FIXED_TITLE = 30

  export default {
    props: {
      data: {
        type: Array,
        default: null
      }
    },
    data () {
      return {
        // 实时响应，参数3
        probeType: 3,
        // 滚动的距离
        scrollY: -1,
        // 当前右侧列表的位置
        currentIndex: 0,
        // fiexed标题和它下面的标题的距离
        diff: -1
      }
    },
    created () {
      // 保存手指滑动的坐标
      this.touch = {}
      // 监听滚动事件
      this.listenScroll = true
      // 每个歌手对应的高度
      this.listHeight = []
    },
    methods: {
      // 点击歌手详细信息时，派发点击事件
      selectItem (item) {
        // 把select事件及被点击的DOM派发出去
        this.$emit('select', item)
      },
      refresh () {
        this.$refs.listview.refresh()
      },
      onShortcutTouchStart (e) {
        // 找到被点击的索引是第几个
        let anchorIndex = e.target.getAttribute('data-index')
        // touch对象
        let firstIndex = e.touches[0]
        // 手指滑动时的纵坐标
        this.touch.y1 = firstIndex.pageY
        this.touch.anchorIndex = anchorIndex
        // 滚动到被点击索引对应的列表的第一个子元素的位置
        this._scrollToElement(anchorIndex)
      },
      // 手指滑动时的处理函数
      onShortcutTouchMove (e) {
        // touch对象
        let firstIndex = e.touches[0]
        // 手指滑动时的纵坐标
        this.touch.y2 = firstIndex.pageY
        // 滑动了几个列表元素
        let delta = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0
        // 需要滚动到的列表元素位置
        // console.log(this.touch.anchorIndex)
        let anchorIndex = parseInt(this.touch.anchorIndex) + delta
        // 滚动到被点击索引对应的列表的第一个子元素的位置
        this._scrollToElement(anchorIndex)
      },
      // 右侧列表随左侧滑动而高亮
      scroll (pos) {
        this.scrollY = pos.y
      },
      // 计算每个歌手对应的高度
      _calculateHeight () {
        this.listHeight = []
        const list = this.$refs.listGroup
        let height = 0
        this.listHeight.push(height)
        for (let i = 0; i < list.length; i++) {
          let item = list[i]
          height += item.clientHeight
          this.listHeight.push(height)
        }
      },
      // 滚动到相应位置
      _scrollToElement (index) {
        // 向上滑动和向下滑动超过右侧列表元素范围时
        if (index < 0) {
          index = 0
        } else if (index > this.listHeight.length - 2) {
          index = this.listHeight.length - 2
        }
        this.scrollY = -this.listHeight[index]
        this.$refs.listview.scrollToElement(this.$refs.listGroup[index], 400)
      }
    },
    computed: {
      // 拿到歌手分类标题
      shortcutList () {
        // 遍历data，拿到tilte
        return this.data.map((group) => {
          // 热门只截取'热'，所以截取0-1位
          return group.title.substr(0, 1)
        })
      },
      // 滚动列表时固定的标题
      fixTilte () {
        if (this.scrollY > 0) {
          return ''
        }
        return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ''
      }
    },
    watch: {
      // 当前歌手变换时计算出每个歌手对应在页面的高度
      currentIndex () {
        // 计算出每个歌手对应在页面的高度
        this._calculateHeight()
      },
      scrollY (newY) {
        const listHeight = this.listHeight
        // 当滚动到顶部,此时newY > 0
        if (newY > 0) {
          this.currentIndex = 0
          return
        }
        // 当在中间滚动
        for (let i = 0; i < listHeight.length - 1; i++) {
          let height1 = listHeight[i]
          let height2 = listHeight[i + 1]
          if (-newY >= height1 && -newY < height2) {
            this.currentIndex = i
            // fixed标题和它下面这个标题的高度差
            this.diff = height2 + newY
            return
          }
        }
        // 当滚动到底部
        this.currentIndex = listHeight.length - 2
      },
      // 监测diff的变化
      diff (newVal) {
        // 计算出fixed标题需要移动的距离
        // 如果diff大于标题高度就移动，否则不移动
        let fixedTop = (newVal > 0 && newVal < FIXED_TITLE) ? newVal - FIXED_TITLE : 0
        // 为0时就不移动了
        if (this.fixedTop === fixedTop) {
          return
        }
        this.fixedTop = fixedTop
        this.$refs.fixedTitle.style.transform = `translate3d(0,${fixedTop}px,0)`
      }
    },
    components: {
      Scroll,
      Loading
    }
  }
</script>

<style scoped lang="stylus">
  @import "~common/stylus/variable.styl"

  .listview
    position: relative
    width: 100%
    height: 100%
    overflow: hidden
    background: $color-background

    .list-group
      padding-bottom: 30px

      .list-group-title
        height: 30px
        line-height: 30px
        padding-left: 20px
        font-size: $font-size-small
        color: $color-text-l
        background: $color-highlight-background

      .list-group-item
        display: flex
        align-items: center
        padding: 20px 0 0 30px

        .avatar
          width: 50px
          height: 50px
          border-radius: 50%

        .name
          margin-left: 20px
          color: $color-text-l
          font-size: $font-size-medium

    .list-shortcut
      position: absolute
      z-index: 30
      right: 0
      top: 50%
      width: 20px
      padding: 20px 0
      text-align: center
      transform: translateY(-50%)
      border-radius: 10px
      background-color: $color-background-d
      font-family: Helvetica

      .item
        padding: 3px
        line-height: 1
        color: $color-text-l
        font-size: $font-size-small

        &.current
          color: $color-theme

    .list-fixed
      position: absolute
      left: 0
      top: -2px
      width: 100%

      .fixed-title
        height: 30px
        line-height: 30px
        padding-left: 20px
        font-size: $font-size-small
        color: $color-text-l
        background: $color-highlight-background

    .loading-container
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>
